package com.ghsc.ghxc.jsb.ui.my.view

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.CheckBox
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.allen.library.shape.ShapeTextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.ui.my.bean.BackCartDataBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.home.view
 * 类名：PayViewPopup
 * 创建时间：2023/7/7 10:53
 * 创建人 @author： xiaolin
 * 描述 ：Todo... 银行卡列表选择
 */
class PayCardListViewPopup (context: AppCompatActivity, list: MutableList<BackCartDataBean>, clickListener: ConfirmClickListener) : BottomPopupView(context) {
    override fun getImplLayoutId() = R.layout.pay_card_view
    private val mContext = context
    private var clickListener: ConfirmClickListener? = null
    private  val mList =list

    interface ConfirmClickListener {
        fun onConfirm(bean: BackCartDataBean)
    }

    init {
        this.clickListener = clickListener
    }
    val mAdapter = ListAdapter()
    var checkBackCartDataBean:BackCartDataBean?=null
    override fun onCreate() {
        super.onCreate()
        var  rvList= findViewById<RecyclerView>(R.id.rvList)
        rvList.layoutManager = LinearLayoutManager(mContext)
        rvList.adapter = mAdapter
        mAdapter.replaceData(mList)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            mAdapter.data.map { it.isCheck=false }
            mAdapter.data[position].isCheck=true
            checkBackCartDataBean=mAdapter.data[position]
            mAdapter.notifyDataSetChanged()
        }

        findViewById<ShapeTextView>(R.id.btnSubmit).setSingleClickListener {
            for (i in 0 until mAdapter.data.size){
                if(mAdapter.data[i].isCheck){
                    checkBackCartDataBean=mAdapter.data[i]
                    break
                }
            }
            if (checkBackCartDataBean!=null){
                clickListener!!.onConfirm(checkBackCartDataBean!!)
                dismiss()
            }else{
                showToast("请选择银行卡")
            }
        }
        findViewById<ShapeTextView>(R.id.btnDismiss).setSingleClickListener {
            dismiss()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
    override fun getMaxWidth() = 0
    class ListAdapter  : BaseQuickAdapter<BackCartDataBean, BaseViewHolder>(
        R.layout.pay_card_item) {
        override fun convert(helper: BaseViewHolder, item: BackCartDataBean) {
            helper.setText(R.id.tvName,item.card_name+"  "+item.bank_name)
                .setText(R.id.cartNo, item.card_type+"  "+item.card_id)
            helper.getView<ImageView>(R.id.ivSelect).setImageResource(if (item.isCheck) R.drawable.check_true_red else R.drawable.check_false_red)


        }

    }
}